using System;
using System.Collections.Generic;
using System.Text;

using MoneyManagerPro.Business.Entities;
using System.Data;

namespace MoneyManagerPro.Business.Logic
{
    public class Users
    {
        private static string mConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

        #region Create methods

        public static User CreateUser(User user)
        {
            Resources.Repository.Users.UserRepository ur =
             new Resources.Repository.Users.UserRepository(mConnectionString);
            ur.Add(user);
            return user;
        }

        public static User CreateUser(string firstName, string lastName, string userName, string password, string email, DateTime lastVisit, string description)
        {
            User user = new User(-1, firstName, lastName, userName, password, email, lastVisit, description);

            Resources.Repository.Users.UserRepository ur =
             new Resources.Repository.Users.UserRepository(mConnectionString);
            ur.Add(user);
            return user;
        }

        #endregion

        #region Read methods

        public static User GetUserById(int id)
        {
            Resources.Repository.Users.UserRepository ur =
                new Resources.Repository.Users.UserRepository(mConnectionString);
            return ur.GetUserById(id);
        }

        public static List<MoneyManagerPro.Business.Entities.User> GetUsers()
        {
            Resources.Repository.Users.UserRepository ur =
                new Resources.Repository.Users.UserRepository(mConnectionString);
            return ur.GetAllFromUsers();
        }

        public static DataTable GetUsersDT()
        {
            List<MoneyManagerPro.Business.Entities.User> list = GetUsers();

            DataTable dt = new DataTable("Users");

            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("LastVisit", typeof(DateTime));
            dt.Columns.Add("Description", typeof(string));

            foreach (MoneyManagerPro.Business.Entities.User n in list)
            {
                dt.Rows.Add(new object[] { n.Id, n.UserName, n.FirstName, n.LastName, n.Email, n.LastVisit, n.Description });
            }

            return dt;
        }

        #endregion

        #region Update methods

        public static User UpdateUser(User user)
        {
            Resources.Repository.Users.UserRepository ur =
                new Resources.Repository.Users.UserRepository(mConnectionString);
            ur.Update(user);
            return user;
        }

        #endregion

        #region Delete methods

        public static void DeleteUser(int id)
        {
            Resources.Repository.Users.UserRepository ur =
                new Resources.Repository.Users.UserRepository(mConnectionString);
            ur.Remove(id);
        }

        #endregion
    }
}